package ecomp.uefs.micc.sharedbox.view;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.commons.mail.EmailException;

import ecomp.uefs.micc.sharedbox.model.Server;

@SuppressWarnings("serial")
public class ServerFrame extends JFrame {
	
	private Server server;
	
	public ServerFrame(){		
		server = new Server();
		initComponents();
	}
	
	public static void main(String[] args){
		ServerFrame window = new ServerFrame();
		}
	
	// Variables Declaration
		private JLabel labelLog;
		private JTextArea log;
		private JScrollPane scroll;
		private JButton btnConnect;
		private JButton btnAbort;
		private JButton btnExit;
		private JButton btnSend;
	
	/**
	 * 
	 */
	
	private void initComponents(){
				
		Container pane = this.getContentPane(); //captura o painel do JFrame
		pane.setLayout(new FlowLayout(FlowLayout.CENTER)); //define o layout
		
	// LabelText
		labelLog = new JLabel("Server Log:");
		pane.add(labelLog);
		
	// TextArea
		log = new JTextArea(15,30);
		
	// ScrollPane
		scroll = new JScrollPane(log);
	
		scroll.setHorizontalScrollBarPolicy(		// Define a política de utilização da barra de rolagem horizontal
		JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		scroll.setVerticalScrollBarPolicy(			// Define a política de utilização da barra de rolagem vertical
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		pane.add(scroll);
		
	// Buttons
		btnConnect = new JButton("Start");
		btnAbort = new JButton("Abort");
		btnSend = new JButton("Send");
		btnExit = new JButton("Exit");
		
		btnConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConnectActionPerformed(evt);
            }
        });
		
		btnAbort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbortActionPerformed(evt);
            }
        });
		
		btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }		
        });
		
		btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					btnSendActionPerformed(evt);
				} catch (EmailException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }					
        });
				
		pane.add(btnConnect);
		pane.add(btnAbort);
		pane.add(btnSend);
		pane.add(btnExit);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400, 320);
		this.setVisible(true);
		this.setResizable(false);
		this.setTitle("File Server");
		setLocationRelativeTo(null);
		
	}
	
	/**
	 * 
	 * @param evt
	 */
	private void btnConnectActionPerformed(java.awt.event.ActionEvent evt){
		checkState();
	}
	
	/**d
	 * 
	 * @param evt
	 */
	private void btnAbortActionPerformed(java.awt.event.ActionEvent evt){
		if(server.getState() == true){
			server.stop();
			server.setState(false);
			btnConnect.setEnabled(true);
			JOptionPane.showMessageDialog(null, "Server Stopped ", "Server Stopped", 1);			
		} else {
			JOptionPane.showMessageDialog(null, "Server is Already Stopped ", "Server Stopped", 1);
		}	
		
	}
	
	private void btnExitActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		if(server.getState() == true){
			int answer = JOptionPane.showConfirmDialog(this,"Server is Running. Want to stop it?","Stop",
					JOptionPane.OK_CANCEL_OPTION,
					JOptionPane.QUESTION_MESSAGE);
			if(answer == JOptionPane.OK_OPTION){
				server.stop();
				server.setState(false);
				System.exit(0);
			}
		} else{
				int answer = JOptionPane.showConfirmDialog(this,"Quit?","Exit",
					JOptionPane.OK_CANCEL_OPTION,
					JOptionPane.QUESTION_MESSAGE);
						if(answer == JOptionPane.OK_OPTION){				
							System.exit(0);
						}
		}	
	}
	
	private void btnSendActionPerformed(ActionEvent evt) throws EmailException {
		// TODO Auto-generated method stub
		server.sendEmail();
	}
	
	private void checkState(){
		if(server.getState() == false){
			server.start();
			server.setState(true);
			btnConnect.setEnabled(false);
		
		} else{
			JOptionPane.showMessageDialog(null, "Server is Already Running on Port " + server.getPort(), "Server Runing", 1);
		}
	}

}
